package com.readboy.calculator;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.ginsmile.calculatorpro.R;

public class MainActivity extends AppCompatActivity {
    private final String TAG="MainActivity";

    private String expression = "";
    private boolean last_equal = false;//上一次的按键是否为等号

    protected EditText text1;//第一行，用来显示按过等号之后的完整表达式
    protected EditText text2;//第二行，用来显示表达式和结果
    protected static boolean isSimple = true;//当前是否是简易计算器

    private View board;
    private View board2;

    private int screen_width;
    private int screen_height;

    private LinearLayout display;
    private Button[] buttons;
    private Button[] buttons2;

    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;

    private ImageButton shrink;
    private ImageButton enlarge;
//    private Button drag_btn;

    private  Intent intent;

    private int angle_metric=0;
    FlowService.MyBinder binder;
    private ServiceConnection conn=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e(TAG,"onServiceConnected执行");
            binder= (FlowService.MyBinder) service;
//            String tmp=expression;
//            Log.e(TAG,"tmp="+tmp);
            expression=binder.getExpression();
            text2.setText(expression);
            Log.e(TAG,"expression="+expression);
//            binder.setExpression(tmp);



//            binder.setExpression(expression);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(TAG,"断开连接");
            binder=null;
        }
    };


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (!Settings.canDrawOverlays(this)){
            Toast.makeText(this,"授权失败",Toast.LENGTH_SHORT).show();
        }else {
            Log.e(TAG,"onActivityResult");
            Toast.makeText(this,"授权成功",Toast.LENGTH_SHORT).show();
            startService(new Intent(MainActivity.this,FlowService.class));
//            bindService(new Intent(MainActivity.this,FlowService.class),conn, Service.BIND_AUTO_CREATE);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void startFlowService(View view){
        if (FlowService.isStarted){
            Log.e(TAG,"返回");
            return;
        }
        if (!Settings.canDrawOverlays(this)){
            Toast.makeText(this,"当前无权限，请授权",Toast.LENGTH_SHORT).show();
            startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:"+getPackageName())),0);
        }else {
//            Toast.makeText(this,"授权成功",Toast.LENGTH_SHORT).show();
//            Log.e("jay","startFlowService");
            intent=new Intent(MainActivity.this,FlowService.class);
            intent.putExtra("largeExpression",expression);
            startService(intent);
            bindService(intent,conn,Service.BIND_AUTO_CREATE);
            Log.e(TAG,"startFlowService:expression="+expression);
            Log.e(TAG,"binder="+binder);
//            binder.setExpression(expression);
            Log.e(TAG,"startFlowService:intent="+intent);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e(TAG,"onCreate");
        Intent in=getIntent();

        String s=in.getStringExtra("shrinkExpression");
        if (s!=null){
            expression=s;
        }
        Log.e(TAG,"expression="+expression);

        boolean serviceRunning=ServiceUtils.isServiceRunning(MainActivity.this,"FlowService");

        if (serviceRunning){
            intent=new Intent(MainActivity.this,FlowService.class);
            bindService(intent,conn,Service.BIND_AUTO_CREATE);
            Log.e(TAG,"onCreate:binder="+binder);
//            expression=binder.getExpression();
//            text2.setText(expression);
        }
//        Log.e("jay","serviceRunning="+serviceRunning);
//        if (binder!=null){
//            expression=binder.getExpression();
//            text2.setText(expression);
//        }
//            stopService(new Intent(MainActivity.this,FlowService.class));
//        Log.e("jay","onCreate:intent="+intent);
        Button change_btn = (Button)findViewById(R.id.change);
//        drag_btn=findViewById(R.id.btn_drag);


        shrink=findViewById(R.id.shrink_btn);
        enlarge=findViewById(R.id.enlarge_btn);


        change_btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                //更换键盘
                if(isSimple == true){
                    //缩放动画效果
                    board.setVisibility(View.INVISIBLE);
                    board2.setVisibility(View.VISIBLE);
                    ScaleAnimation sa = new ScaleAnimation(1.2f,1f,1.2f,1f,
                            Animation.RELATIVE_TO_SELF,1f,
                            Animation.RELATIVE_TO_SELF,1f);
                    sa.setDuration(300);
                    board2.startAnimation(sa);
                    isSimple = false;
                }else{
                    ScaleAnimation sa = new ScaleAnimation(1f,1.25f,1f,1.2f,
                            Animation.RELATIVE_TO_SELF,1f,
                            Animation.RELATIVE_TO_SELF,1f);
                    sa.setDuration(300);
                    board2.startAnimation(sa);

                    board2.setVisibility(View.INVISIBLE);
                    board.setVisibility(View.VISIBLE);
                    isSimple = true;
                }
            }
        });


        shrink.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View v) {
//                Log.e("jay","MainActivity:shrink");
                startFlowService(v);
                finish();
            }
        });


        text1 = (EditText)findViewById(R.id.text1);
        text2 = (EditText)findViewById(R.id.text2);

        text2.setText(expression);
        text2.setTextSize(getResources().getDimension(R.dimen.editBig));

        //初始化计算器键盘
        buttons = new Button[18];
        buttons2 = new Button[31];
        initSimpleBoard(buttons);//初始化简易计算器键盘
        initScienceBoard(buttons2);//初始化科学计算器键盘
        board = (View)findViewById(R.id.board);
        board2 = (View)findViewById(R.id.board2);


        if(savedInstanceState != null){
            text1.setText(savedInstanceState.getString("text1"));
            text2.setText(savedInstanceState.getString("text2"));
            isSimple = savedInstanceState.getBoolean("isSimple");
            Log.v("TAG==>","OKKOKOKO");
        }

//        initFlowWindow();
    }

    //初始话悬浮窗
    private void initFlowWindow(){
        windowManager= (WindowManager) getSystemService(WINDOW_SERVICE);
        layoutParams=new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }

        layoutParams.format= PixelFormat.RGBA_8888;
        layoutParams.gravity= Gravity.LEFT|Gravity.TOP;
        layoutParams.flags=WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.width=800;
        layoutParams.height=1800;
        layoutParams.x=300;
        layoutParams.y=300;
    }

    //活动被回收时，保存临时数据
    @Override
    protected void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);
        outState.putString("text1", text1.getText().toString());
        outState.putString("text2", text2.getText().toString());
        outState.putBoolean("isSimple",isSimple);

    }


    //为了得到用户区域的高度，重写onWindowFocusChanged,这个方法在onResume之后被调用
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus){
            Dimension dimen1 = getAreaOne(this);
            Dimension dimen2 = getAreaTwo(this);
            Dimension dimen3 = getAreaThree(this);
            Log.v("one=>","Area one : \n\tWidth: "+dimen1.mWidth + ";\tHeight: "+dimen1.mHeight);
            Log.v("two=>","\nArea two: \n\tWidth: "+dimen2.mWidth + ";\tHeight: "+dimen2.mHeight);
            Log.v("three","\nArea three: \n\tWidth: "+dimen3.mWidth + ";\tHeight: "+dimen3.mHeight);

            Log.v("TAG","---isSimple=>>" + isSimple);
            screen_width = dimen3.mWidth;
            screen_height = dimen3.mHeight;

            initWidthAndHeight();
        }
    }

    //初始化键盘，显示区域的宽和高（显示区域包括change按钮，text，text2）
    private void initWidthAndHeight(){
        //设置change按钮和显示区域的高度只和 始终为用户区域高度的三分之一
        display  = (LinearLayout)findViewById(R.id.display);
        android.view.ViewGroup.LayoutParams lp =display.getLayoutParams();
        lp.height=screen_height/3;

        //简易计算器
        int btn_width = screen_width/4;
        int btn_height = (screen_height - screen_height/3)/5;//tablelayout为屏幕的2/3大，一共5行
        for(int i= 0; i < 18; i++){
            buttons[i].setWidth(btn_width);
            buttons[i].setHeight(btn_height);
        }

        buttons[0].setWidth(btn_width*2);
        buttons[16].setHeight(btn_height*2);

        //科学计算器
        //让每个科学计算器的按钮的高度为tablelayout的1/6
        for(int i = 0; i < buttons2.length; i++) {
            buttons2[i].setHeight(screen_height*2/3/6);
        }
    }

    //初始化简易计算器键盘
    private void initSimpleBoard(final Button[] buttons){
        buttons[0] = (Button)findViewById(R.id.zero);
        buttons[1] = (Button)findViewById(R.id.one);
        buttons[2] = (Button)findViewById(R.id.two);
        buttons[3] = (Button)findViewById(R.id.three);
        buttons[4] = (Button)findViewById(R.id.four);
        buttons[5] = (Button)findViewById(R.id.five);
        buttons[6] = (Button)findViewById(R.id.six);
        buttons[7] = (Button)findViewById(R.id.seven);
        buttons[8] = (Button)findViewById(R.id.eight);
        buttons[9] = (Button)findViewById(R.id.nine);

        buttons[10] = (Button)findViewById(R.id.empty);
        buttons[11] = (Button)findViewById(R.id.delete);
        buttons[12] = (Button)findViewById(R.id.divide);
        buttons[13] = (Button)findViewById(R.id.multiple);
        buttons[14] = (Button)findViewById(R.id.minus);
        buttons[15] = (Button)findViewById(R.id.plus);
        buttons[16] = (Button)findViewById(R.id.equal);
        buttons[17] = (Button)findViewById(R.id.dot);


        initCommonBtns(buttons);
    }


    //初始化科学计算器键盘
    private void initScienceBoard(final Button[] buttons){
        buttons[0] = (Button)findViewById(R.id.zero2);
        buttons[1] = (Button)findViewById(R.id.one2);
        buttons[2] = (Button)findViewById(R.id.two2);
        buttons[3] = (Button)findViewById(R.id.three2);
        buttons[4] = (Button)findViewById(R.id.four2);
        buttons[5] = (Button)findViewById(R.id.five2);
        buttons[6] = (Button)findViewById(R.id.six2);
        buttons[7] = (Button)findViewById(R.id.seven2);
        buttons[8] = (Button)findViewById(R.id.eight2);
        buttons[9] = (Button)findViewById(R.id.nine2);

        buttons[10] = (Button)findViewById(R.id.empty2);
        buttons[11] = (Button)findViewById(R.id.delete2);
        buttons[12] = (Button)findViewById(R.id.divide2);
        buttons[13] = (Button)findViewById(R.id.multiple2);
        buttons[14] = (Button)findViewById(R.id.minus2);
        buttons[15] = (Button)findViewById(R.id.plus2);
        buttons[16] = (Button)findViewById(R.id.equal2);
        buttons[17] = (Button)findViewById(R.id.dot2);

        initCommonBtns(buttons);


        //初始化剩余的12个按钮
        buttons[18] = (Button)findViewById(R.id.sin);
        buttons[19] = (Button)findViewById(R.id.cos);
        buttons[20] = (Button)findViewById(R.id.tan);
        buttons[21] = (Button)findViewById(R.id.ln);
        buttons[22] = (Button)findViewById(R.id.log);

        buttons[23] = (Button)findViewById(R.id.rad);
        buttons[30]=  (Button)findViewById(R.id.deg);

        buttons[24] = (Button)findViewById(R.id.power);
        buttons[25] = (Button)findViewById(R.id.sqrt);
        buttons[26] = (Button)findViewById(R.id.pi);
        buttons[27] = (Button)findViewById(R.id.left_parentheses);
        buttons[28] = (Button)findViewById(R.id.right_parentheses);
        buttons[29] = (Button)findViewById(R.id.e);

        for (int i=0;i<=30;i++){
            buttons[i].setTextSize(getResources().getDimension(R.dimen.btn_text_size));
        }

        buttons[18].setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if(last_equal){
                    expression = "";//这次按的数字，如果上次按了等号，则清空表达式
                    last_equal = false;
                }
                expression += buttons[18].getText() + "(";
                text2.setText(expression);
                text2.setSelection(expression.length());
                last_equal = false;
            }
        });
        buttons[19].setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if(last_equal){
                    expression = "";//这次按的数字，如果上次按了等号，则清空表达式
                    last_equal = false;
                }
                expression += buttons[19].getText() + "(";
                text2.setText(expression);
                text2.setSelection(expression.length());
                last_equal = false;
            }
        });
        buttons[20].setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if(last_equal){
                    expression = "";//这次按的数字，如果上次按了等号，则清空表达式
                    last_equal = false;
                }
                expression += buttons[20].getText() + "(";
                text2.setText(expression);
                text2.setSelection(expression.length());
                last_equal = false;
            }
        });
        buttons[21].setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if(last_equal){
                    expression = "";//这次按的数字，如果上次按了等号，则清空表达式
                    last_equal = false;
                }
                expression += buttons[21].getText() + "(";
                text2.setText(expression);
                text2.setSelection(expression.length());
                last_equal = false;
            }
        });
        buttons[22].setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if(last_equal){
                    expression = "";//这次按的数字，如果上次按了等号，则清空表达式
                    last_equal = false;
                }
                expression += buttons[22].getText() + "(";
                text2.setText(expression);
                text2.setSelection(expression.length());
                last_equal = false;
            }
        });
        buttons[23].setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                buttons[23].setBackgroundColor(getResources().getColor(R.color.keyBackground_pressed));
                buttons[23].setEnabled(false);
                buttons[30].setEnabled(true);
                buttons[30].setBackgroundColor(getResources().getColor(R.color.keyBackground_normal));
                angle_metric=1;
            }
        });

        buttons[30].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttons[30].setBackgroundColor(getResources().getColor(R.color.keyBackground_pressed));
                buttons[30].setEnabled(false);
                buttons[23].setBackgroundColor(getResources().getColor(R.color.keyBackground_normal));
                buttons[23].setEnabled(true);
                angle_metric=0;
            }
        });

        buttons[24].setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if(last_equal){
                    expression = "";//这次按的数字，如果上次按了等号，则清空表达式
                    last_equal = false;
                }
                expression += buttons[24].getText();
                text2.setText(expression);
                text2.setSelection(expression.length());
                last_equal = false;
            }
        });
        buttons[25].setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if(last_equal){
                    expression = "";//这次按的数字，如果上次按了等号，则清空表达式
                    last_equal = false;
                }
                expression += buttons[25].getText()+"(";
                text2.setText(expression);
                text2.setSelection(expression.length());
                last_equal = false;
            }
        });
        buttons[26].setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if(last_equal){
                    expression = "";//这次按的数字，如果上次按了等号，则清空表达式
                    last_equal = false;
                }
                expression += buttons[26].getText();
                text2.setText(expression);
                text2.setSelection(expression.length());
                last_equal = false;
            }
        });
        buttons[27].setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if(last_equal){
                    expression = "";//这次按的数字，如果上次按了等号，则清空表达式
                    last_equal = false;
                }
                expression += buttons[27].getText();
                text2.setText(expression);
                text2.setSelection(expression.length());
                last_equal = false;
            }
        });
        buttons[28].setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if(last_equal){
                    expression = "";//这次按的数字，如果上次按了等号，则清空表达式
                    last_equal = false;
                }
                expression += buttons[28].getText();
                text2.setText(expression);
                text2.setSelection(expression.length());
                last_equal = false;
            }
        });
        buttons[29].setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if(last_equal){
                    expression = "";//这次按的数字，如果上次按了等号，则清空表达式
                    last_equal = false;
                }
                expression += buttons[29].getText();
                text2.setText(expression);
                text2.setSelection(expression.length());
                last_equal = false;
            }
        });
    }


    //初始化简易计算器，科学计算器相同的18个按钮
    private void initCommonBtns(final Button[] buttons){
        //添加监听事件
        //数字0～9
        for(int i = 0; i < 10; i++){
            final int m = i;
            buttons[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(last_equal){
                        expression = "";//这次按的数字，如果上次按了等号，则清空表达式
                        last_equal = false;
                    }
                    expression += buttons[m].getText();
                    text2.setText(expression);
                    text2.setSelection(expression.length());
                }
            });
        }
        //empty
        buttons[10].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                expression = "";
                text2.setText("0");
                text1.setText(null);
                last_equal = false;
            }
        });
        //delete
        buttons[11].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(expression.length() < 1){
                    return;
                }
                expression = expression.substring(0,expression.length()-1);
                text2.setText(expression);
                text2.setSelection(expression.length());
                last_equal = false;
            }
        });
        //divide
        buttons[12].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                expression += buttons[12].getText();
                text2.setText(expression);
                text2.setSelection(expression.length());
                last_equal = false;
            }
        });
        //multiple
        buttons[13].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                expression += buttons[13].getText();
                text2.setText(expression);
                text2.setSelection(expression.length());
                last_equal = false;
            }
        });
        //minus
        buttons[14].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                expression += buttons[14].getText();
                text2.setText(expression);
                text2.setSelection(expression.length());
                last_equal = false;
            }
        });
        //plus
        buttons[15].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                expression += buttons[15].getText();
                text2.setText(expression);
                text2.setSelection(expression.length());
                last_equal = false;
            }
        });
        //equal
        buttons[16].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(last_equal) return;//如果上次还是按的等号，那么什么也不做

                //小小的动画效果
                AnimationSet animSet = new AnimationSet(true);
                TranslateAnimation ta = new TranslateAnimation(0,0,0,-100);
                ta.setDuration(80);
                AlphaAnimation aa = new AlphaAnimation(1f, 0f);
                aa.setDuration(75);
                animSet.addAnimation(ta);
                animSet.addAnimation(aa);
                text2.startAnimation(animSet);
                animSet.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        //动画结束之后计算
//                        text1.setText(expression + "=");
//                        text1.setSelection(expression.length()+1);//在第一行显示计算表达式
//                        try{
//                            expression = Calculate.calculate(expression);
//                            text2.setText(expression);//在第二行显示计算结果
//                        }catch(Exception exception){
//                            text2.setText("表达式错误!");//在第二行显示计算结果
//                            expression = "";
//                        }
                        int leftNum = 0;
                        int rightNum = 0;
                        for (int i = 0; i < expression.length(); i++) {
                            if (expression.charAt(i) == '(')
                                leftNum++;
                            if (expression.charAt(i) == ')')
                                rightNum++;
                        }
                        int missingNum = leftNum - rightNum; //缺失的 ) 数量
                        while (missingNum > 0) {
                            expression += ')';
                            missingNum--;
                        }
                        text1.setText(expression+"=");

//                        mathPast = "\n" + mathNow; //使得呈现的mathPast自动换行

                        ScienceCalculator scienceCalculator=new ScienceCalculator();
                        try{
                            double result = scienceCalculator.cal(expression, 6, angle_metric); //调用科学计算器

                            if (result == Double.MAX_VALUE)
                                expression = "Math Error";
                            else {
                                expression = String.valueOf(result);
                                if (expression.charAt(expression.length() - 2) == '.' && expression.charAt(expression.length() - 1) == '0') {
                                    expression = expression.substring(0, expression.length() - 2);
                                }
                            }

                            text2.setText(expression);

                        }catch (Exception e){
                            text2.setText("表达式错误");
                            expression="";
                        }


//                        mathPast = mathPast + "=" + mathNow;

                        //用tvPast.set(mathPast)不能实现自动滚动到最新运算过程
//                        tvPast.append(mathPast); //添加新的运算过程

                        //tvPast滚动到最新的运算过程
//                        int offset = tvPast.getLineCount() * tvPast.getLineHeight();
//                        if (offset > tvPast.getHeight()) {
//                            tvPast.scrollTo(0, offset - tvPast.getHeight());
//                        }
//                        tvNow.setText(mathNow);


                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });


                // 为下一次按计算器键盘做准备。
                // 如果下次按的是数字，那么清空第二行重新输入第一个数。
                // 如果是非数字，那就当这次的结果是输入的第一个数，直接参与运算。
                last_equal = true;

            }


        });
        buttons[17].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                expression += buttons[17].getText();
                text2.setText(expression);
                text2.setSelection(expression.length());
                last_equal = false;
            }
        });
    }



    //屏幕高度
    private Dimension getAreaOne(Activity activity){
        Dimension dimen = new Dimension();
        Display disp = activity.getWindowManager().getDefaultDisplay();
        Point outP = new Point();
        disp.getSize(outP);
        dimen.mWidth = outP.x ;
        dimen.mHeight = outP.y;
        return dimen;
    }
    //不算状态栏的高度
    private Dimension getAreaTwo(Activity activity){
        Dimension dimen = new Dimension();
        Rect outRect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
        System.out.println("top:"+outRect.top +" ; left: "+outRect.left) ;
        dimen.mWidth = outRect.width() ;
        dimen.mHeight = outRect.height();
        return dimen;
    }
    //不算状态栏，标题栏的高度
    private Dimension getAreaThree(Activity activity){
        Dimension dimen = new Dimension();
        // 用户绘制区域
        Rect outRect = new Rect();
        activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);
        dimen.mWidth = outRect.width() ;
        dimen.mHeight = outRect.height();
        // end
        return dimen;
    }
    private class Dimension {
        public int mWidth ;
        public int mHeight ;
        public Dimension(){}
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
//        unbindService(conn);
        Log.e(TAG,"onDestroy");
    }

    @Override
    protected void onStop() {
        super.onStop();
//        if (expression!="")
//            binder.setExpression(expression);
        Log.e(TAG,"onStop:binder="+binder);
        Log.e(TAG,"onStop");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(TAG,"onRestart");
        Log.e(TAG,"onRestart:intent="+intent);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG,"onStart");
    }

    @Override
    protected void onPause() {
        super.onPause();
//        binder.setExpression(expression);
        Log.e(TAG,"onPause");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG,"onResume:intent="+intent);
//        if (FlowService.isStarted){
//            Log.e("jay","onResume:intent="+intent);
//            Log.e("jay","isStarted="+FlowService.isStarted);
////            stopService(intent);
//
//        }
        if (binder!=null){
            Log.e("jay","onResume:binder="+binder+"expression="+expression);
            expression = binder.getExpression();
            text2.setText(expression);
        }

        Log.e(TAG,"onResume:binder="+binder+"expression="+expression);

//        Log.e("jay","onResume:isStarted="+FlowService.isStarted);
    }
}


